from fastapi import APIRouter, Depends
from sqlalchemy.ext.asyncio import AsyncSession

import crud
from api import dependencies
from core.exceptions import UserWithThatUserIdNotFoundException
from schema.response import IGetResponseBase, IPostResponseBase, IPutResponseBase
from schema.user import IUserRead, IUserRegister

router = APIRouter()

# @router.get('/me', response_model=IGetResponseBase[IUserRead])
# async def get_user_by_id(curr_user: User = Depends(dependencies.get_current_user())):
#     return IGetResponseBase[IUserRead](data=curr_user)


# @router.post('/register', response_model=IPostResponseBase[IUserRead])
# async def register(user: IUserRegister, db_session: AsyncSession = Depends(dependencies.get_db)):
#     user = IUserRead.model_validate(await crud.user.register(user_schema=user, db_session=db_session))
#     return IPostResponseBase[IUserRead](data=user)
#
#
# @router.get('/{user_id}', response_model=IGetResponseBase[IUserRead])
# async def get_user(user_id: int, db_session: AsyncSession = Depends(dependencies.get_db)):
#     user = await crud.user.get_user_by_id(user_id, db_session)
#     if user:
#         user = IUserRead.model_validate(user)
#         return IGetResponseBase[IUserRead](data=user)
#     else:
#         raise UserWithThatUserIdNotFoundException(user_id)

@router.post('/add/', response_model=IPostResponseBase[IUserRead])
async def add_user(schema: IUserRegister, db_session: AsyncSession = Depends(dependencies.get_db)):
    u = await crud.user.register(schema, db_session)
    u = IUserRead.model_validate(u)
    return IPostResponseBase[IUserRead](data=u)

@router.put('/{user_id}/', response_model=IPutResponseBase[None])
async def add_user(user_id: int, schema: IUserRegister, db_session: AsyncSession = Depends(dependencies.get_db)):
    await crud.user.modify(user_id, schema, db_session)
    return IPostResponseBase[None]